//
//  KColumnTableControl.m
//  MengLiao
//
//  Created by xiuxin on 2022/7/15.
//  Copyright © 2022 ZHBen. All rights reserved.
//

#import "KColumnTableControl.h"

@interface KColumnTableCell : UITableViewCell

@end

@implementation KColumnTableCell

@end

@interface KColumnTableControl()

@property (weak,nonatomic) UITableView * containerView;

@property (nonatomic) NSInteger section;

@property (strong,nonatomic) NSArray<KColumnInfo *> * tableData;

@property (strong,nonatomic) NSMutableArray<NSString *> * reuseIDList;

@end

@implementation KColumnTableControl

- (instancetype)initWithTable:(UITableView *)table section:(NSInteger)section
{
    if (self = [super initWithControl:self])
    {
        _containerView = table;
        _section = section;
        _reuseIDList = [NSMutableArray array];
    }
    return self;
}

- (void)registerCell:(NSArray<KColumnInfo *> *)columnList
{
    for (KColumnInfo * column in columnList)
    {
        NSString * reuseID = @"KColumnTableCell";
        if (column.spec.length > 0)
        {
            reuseID = [reuseID stringByAppendingFormat:@"_%@", column.spec];
        }
        if (![self.reuseIDList containsObject:reuseID])
        {
            [self.containerView registerClass:[KColumnTableCell class] forCellReuseIdentifier:reuseID];
        }
    }
}

- (void)configModel:(KColumnInfo *)model
{
    model.control = self;
    [model refreshItemSize];
}

#pragma mark KColumnListControl Delegate Methods

- (void)reloadColumn:(KColumnInfo *)column
{
    NSInteger index = [self.tableData indexOfObject:column];
    if (index != NSNotFound)
    {
        [self configModel:column];
        [self.containerView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:index inSection:self.section]] withRowAnimation:UITableViewRowAnimationNone];
    }
}

- (void)loadColumnList:(NSArray<KColumnInfo *> *)columnList
{
    [self registerCell:columnList];
    for (KColumnInfo * column in columnList)
    {
        [self configModel:column];
    }
    self.tableData = [NSArray arrayWithArray:columnList];
    [self.containerView reloadSections:[NSIndexSet indexSetWithIndex:self.section] withRowAnimation:UITableViewRowAnimationNone];
    if (columnList.count > 0)
    {
        [self startKVO:self.containerView];
    }
    else
    {
        [self stopKVO];
    }
}

- (void)columnInfo:(KColumnInfo *)column displayPositionChanged:(KDisplayPosition)position
{
    if ([self.delegate respondsToSelector:@selector(columnInfo:displayPositionChanged:)])
    {
        [(id<KColumnListDelegate>)self.delegate column:column displayPositionChanged:position];
    }
}

#pragma mark TableViewDelegate Methods

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSAssert(section == self.section, @"section 不一致！！！");
    return self.tableData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSAssert(indexPath.section == self.section, @"section 不一致！！！");
    if (indexPath.row < self.tableData.count)
    {
        KColumnInfo * column = self.tableData[indexPath.row];
        NSString * reuseID = @"KColumnTableCell";
        if (column.spec.length > 0)
        {
            reuseID = [reuseID stringByAppendingFormat:@"_%@", column.spec];
        }
        KColumnTableCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseID];
        [column cellWillDisplay:cell contentView:cell.contentView];
        return cell;
    }
    return nil;
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSAssert(indexPath.section == self.section, @"section 不一致！！！");
    if (indexPath.row < self.tableData.count)
    {
        KColumnInfo * column = self.tableData[indexPath.row];
        [column cellWillDisplay:cell contentView:cell.contentView];
    }
}

- (void)tableView:(UITableView *)tableView didEndDisplayingCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath*)indexPath
{
    NSAssert(indexPath.section == self.section, @"section 不一致！！！");
    if (indexPath.row < self.tableData.count)
    {
        KColumnInfo * column = self.tableData[indexPath.row];
        [column cellDidEndDisplay:cell];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSAssert(indexPath.section == self.section, @"section 不一致！！！");
    if (indexPath.row < self.tableData.count)
    {
        KColumnInfo * column = self.tableData[indexPath.row];
        return column.itemSize.height;
    }
    return 0;
}

@end
